import Controller from "./Controller";
import {Request,Response} from 'express';
import { exec } from 'child_process';
import { join as path_join } from 'path';
import Config from '../Config'

/**
 * 运行模板项目
 */
class RunTpl extends Controller
{
    constructor(req:Request,res:Response) {
        super(req,res);
    }

    /**
     * 运行项目
     */
    async run(Cache:any) {
        Cache.set("tpl_id",this.HD.tpl_id)
        const Tpl = await this.first("tpl",{id: this.HD.tpl_id}) as {dir:string} 
        await this.runProject(path_join(Config.data_dir,Tpl.dir))
        this.response(200)
    }

    /**
     * 运行项目
     */
    async runProject(project_dir:string):Promise<any> {
        return new Promise((resolve) => {
            exec(`${path_join(Config.root_dir,'node','npm.cmd')} run dev:h5 --prefix=${project_dir}`);
            exec(`${path_join(Config.root_dir,'node','npm.cmd')} run dev:mp-weixin --prefix=${project_dir}`);
            resolve(true)
        })
    }
}

export default RunTpl;